package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IYzrStorehouseDAO;
import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.dao.jpa.LmbConsumableCollarDAO;
import com.trkj.dao.jpa.LmbConsumableOutboundDAO;
import com.trkj.dao.jpa.LmbConsumableOutboundDetailDAO;
import com.trkj.dao.jpa.LmbConsumptiveStockDAO;
import com.trkj.dao.jpa.TbdepartmentDAO;
import com.trkj.pojos.LmbConsumableCollar;
import com.trkj.pojos.LmbConsumableOutbound;
import com.trkj.pojos.LmbConsumableOutboundDetail;
import com.trkj.pojos.LmbConsumptiveStock;
import com.trkj.pojos.Tbdepartment;
import com.trkj.pojos.Tbstaff;
import com.trkj.pojos.YzrStorehouse;
import com.trkj.vo.HcckVo;
import com.trkj.vo.LmbHcckVo;

@Service
@Transactional
public class LmbHcckService {
	@Autowired
	private LmbConsumptiveStockDAO conStockDAO;
	@Autowired
	private LmbConsumableOutboundDAO conOutboundDAO;
	@Autowired
	private LmbConsumableOutboundDetailDAO conOutDetailDAO;
	@Autowired
	private LmbConsumableCollarDAO conCollarDAO;
	@Autowired
	private LmbOddNumbersService oddService;
	@Autowired
	private TbdepartmentDAO departmentDAO;
	@Autowired
	private IYzrTbstaffDAO tbstaffDAO;
	@Autowired
	private IYzrStorehouseDAO storehouseDAO;
	
	public void hcck(LmbHcckVo hcckVo){		
		// 获取日期
		String date = oddService.rq();
		//保存出库记录
		//拼接
		String name = "HCK-" + date;					
		String n = conOutboundDAO.findMaxID(name);	
		System.out.println("从数据库中读取的最大采购计划编号："+n);
		String sid =oddService.getId(name, n);
		LmbConsumableOutbound co =  hcckVo.getConOutbound();
		co.setCoId(sid);
		conOutboundDAO.save(co);
		//修改领用单的领用状态
		LmbConsumableCollar collar = conCollarDAO.findOne(co.getCoVoucherId());
		collar.setCollarCkstatus("已领用");
		conCollarDAO.save(collar);
		//保存出库记录详情
		List<LmbConsumableOutboundDetail> list = hcckVo.getConOutDet();
		System.out.println("list的长度："+list.size());
		if(list != null){	
			//创建出库单与出库详情的关系
			for(LmbConsumableOutboundDetail detail:list){
				//拼接
				String name1 = "HCKX-" + date;	
				String n1 = conOutDetailDAO.findMaxID(name1);	
				String sid1 =oddService.getId(name1, n1);
				detail.setCodId(sid1);
				detail.setCodCoId(sid);
				conOutDetailDAO.save(detail);
				//修改库存数量
				LmbConsumptiveStock stock = conStockDAO.findConStock(detail.getCodCId(), detail.getCodBatchNum());
				//获取的库存数量
				BigDecimal num = stock.getCstocNum();
				//获取的提领数量
				BigDecimal num1 = detail.getCodNum();
				//提领之后的库存数量
				BigDecimal cknum = num.subtract(num1);
				stock.setCstocNum(cknum);
				conStockDAO.save(stock);
			}					
		}	
	}
	
	
	public HcckVo ckPoToVo(LmbConsumableOutbound outbound){
		HcckVo ckVo = new HcckVo();
		ckVo.setCoId(outbound.getCoId());
		ckVo.setCoVoucherId(outbound.getCoVoucherId());
		ckVo.setCoDate(outbound.getCoDate());
		ckVo.setCoBlyck(outbound.getCoBlyck());
		System.out.println(outbound.getCoBlyck());
		YzrStorehouse storehouse = storehouseDAO.findOne(outbound.getCoBlyck());
		if(storehouse==null){
			System.out.println("被领用仓库为空!");
		}
		ckVo.setCockName(storehouse.getStoreName());
		ckVo.setCoObject(outbound.getCoObject());
		Tbdepartment department = departmentDAO.findOne(outbound.getCoObject().longValue());
		ckVo.setCoObjectName(department.getDepartmentName());
		ckVo.setCoOperator(outbound.getCoOperator());
		Tbstaff staffJbr = tbstaffDAO.findOne(outbound.getCoOperator().longValue());
		ckVo.setCoOperatorName(staffJbr.getStaffName());
		ckVo.setCoTlr(outbound.getCoTlr());		
		Tbstaff staffTlr = tbstaffDAO.findOne(outbound.getCoTlr().longValue());
		ckVo.setCoTlrName(staffTlr.getStaffName());
		return ckVo;		
	}
	
	public List<HcckVo> ckPoListToVoList(List<LmbConsumableOutbound> list){
		if(list == null)return null;
		List<HcckVo> listVo = new ArrayList<>();
		for(LmbConsumableOutbound outbound: list){
			HcckVo hcckVo = ckPoToVo(outbound);
			listVo.add(hcckVo);
		}
		return listVo;
	}
	
	public Map<String,Object> findAllck(int page,int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		//获取当前页的记录
		List<LmbConsumableOutbound> list = conOutboundDAO.findOutboundByPage(pr);
		long total = conOutboundDAO.countfindOutboundByPage();
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", ckPoListToVoList(list));
		return map;
	}
	
	//根据出库单查询出库记录
	public HcckVo findCkByckId(String id){
		LmbConsumableOutbound outbound = conOutboundDAO.findOne(id);
		System.err.println(outbound.getCoBlyck());
		return ckPoToVo(outbound);
	}
	
	//根据出库单查询出库详情
	public List<LmbConsumableOutboundDetail> findckxq(String id){
		List<LmbConsumableOutboundDetail> list = conOutDetailDAO.findckxq(id);
		return list;
	}
	
	public Map<String,Object> findCkByID(int page,int rows,String id){
		PageRequest pr = new PageRequest(page - 1, rows);
		//获取当前页的记录
		List<LmbConsumableOutbound> list = conOutboundDAO.findByPage(pr, id);
		long total = conOutboundDAO.countfindByPage(id);
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", ckPoListToVoList(list));
		return map;
	}
}
